﻿NAMFox.ns(function() { with (NAMFox) {

name("Markup Tests");
html("MarkupTests.html");

//////////////////////////////////////////////
// Helper Methods
//////////////////////////////////////////////

var _markupBranch = $.getConfig().markup;

for (var i in _markupBranch) {
    if (_markupBranch.hasOwnProperty(i)) {
        _markupBranch[i] = true;
    }
}

function $F(selector) {
    return $FX(selector, document);
}

function runButtonTest(name, firstExpectation, secondExpectation) {
    var buttons = $F("input[name=" + name + "]");
    equals(2, buttons.length, "There must be exactly two elements selected.");
    
    var t1 = $F("#txtOne").get(0);
    t1.value = "Lorem ipsum";
    t1.setSelectionRange(2, 7);
    click(buttons[0]);
    equals(firstExpectation, t1.value, "The value should equal '" + firstExpectation + "' in the first text area.");

    var t2 = $F("#txtTwo").get(0);
    t2.value = "Ipsum Lorem";
    t2.setSelectionRange(2, 7);
    click(buttons[1]);
    equals(secondExpectation, t2.value, "The value should equal '" + secondExpectation + "' in the second text area.");
}

function runSelectTest(name, action) {
    var selects = $F("select[name=" + name + "]");
    equals(2, selects.length, "There must be exactly two elements selected.");
    
    var t1 = $F("#txtOne").get(0);
    function reset1() {
        t1.value = "Lorem ipsum";
        t1.setSelectionRange(2, 7);
    }
    
    reset1();
    action(selects.get(0), t1, reset1);

    var t2 = $F("#txtTwo").get(0);
    function reset2() {
        t2.value = "Ipsum Lorem";
        t2.setSelectionRange(2, 7);
    }
    
    reset2();
    action(selects.get(1), t2, reset2);
}

//////////////////////////////////////////////
// Test Cases
//////////////////////////////////////////////

test("Adds One Input Element to DOM", function() {
    var div = $F("#txtOneBar");
    div.append(Markup.create($F("#txtOne"), MarkupLocation.Post, Markup.anchor));
    
    equals(2, div.get(0).childNodes.length, "There should be one child node in the div#txtOneBar element.");
    equals("INPUT", div.get(0).childNodes[1].tagName, "The child node's name should be 'INPUT'.");
});

test("Adds One Select Element to DOM", function() {
    var div = $F("#txtTwoBar");
    div.append(Markup.create($F("#txtTwo"), MarkupLocation.Post, Markup.customMessages));
    
    equals(2, div.get(0).childNodes.length, "There should be one child node in the div#txtTwoBar element.");
    equals("SELECT", div.get(0).childNodes[1].tagName, "The child node's name should be 'SELECT'.");
});

test("Adds all markup elements to DOM when they are enabled", function() {
    for (var i in _markupBranch) {
        if (_markupBranch.hasOwnProperty(i)) {
            _markupBranch[i] = true;
        }
    }
    
    var div = $F("#txtOneBar");
    
    div.get(0).innerHTML = "";
    div.append(Markup.create($F("#txtOne"), MarkupLocation.Post, Markup.all));
    equals(Markup.all.length, div.get(0).childNodes.length, "There should be " + Markup.all.length + " child nodes in the div#txtOneBar element.");
});

test("Adds no markup elements to DOM when they are disabled", function() {
    for (var i in _markupBranch) {
        if (_markupBranch.hasOwnProperty(i)) {
            _markupBranch[i] = false;
        }
    }

    var div = $F("#txtOneBar");

    div.get(0).innerHTML = "";
    div.append(Markup.create($F("#txtOne"), MarkupLocation.Post, Markup.all));
    equals(5, div.get(0).childNodes.length, "There should be five child nodes in the div#txtOneBar element.");
});

test("Buttons add correct tags", function() {
    for (var i in _markupBranch) {
        if (_markupBranch.hasOwnProperty(i)) {
            _markupBranch[i] = true;
        }
    }

    var div = $F("#txtOneBar");
    div.html("");
    div.append(Markup.create($F("#txtOne"), MarkupLocation.Post, Markup.all));

    div = $F("#txtTwoBar");
    div.html("");
    div.append(Markup.create($F("#txtTwo"), MarkupLocation.Post, Markup.all));

    runButtonTest(Markup.anchor.name, "Lo[[#rem i]]psum", "Ip[[#sum L]]orem");
    runButtonTest(Markup.code.name, "Lo[code]rem i[/code]psum", "Ip[code]sum L[/code]orem");
    runButtonTest(Markup.horizontalRule.name, "Lo[hr]rem ipsum", "Ip[hr]sum Lorem");
    runButtonTest(Markup.indent.name, "Lo[ul]rem i[/ul]psum", "Ip[ul]sum L[/ul]orem");
    
    var infoProvider = Markup.linkName._informationProvider;
    Markup.linkName._informationProvider = {
        name: function() {
            return "One";
        },
        url: function() {
            return "http://www.neoseeker.com";
        }
    };
    try {
        runButtonTest(Markup.linkName.name, "Lo[link name=rem i]http://www.neoseeker.com[/link]psum", "Ip[link name=sum L]http://www.neoseeker.com[/link]orem");
    }
    finally {
        Markup.linkName._informationProvider = infoProvider;
    }
    
    runButtonTest(Markup.php.name, "Lo[php]rem i[/php]psum", "Ip[php]sum L[/php]orem");
    runButtonTest(Markup.pre.name, "Lo[pre]rem i[/pre]psum", "Ip[pre]sum L[/pre]orem");
    
    infoProvider = Markup.privateMessageLink._informationProvider;
    Markup.privateMessageLink._informationProvider = {
        recipient: function() {
            return "One";
        },
        subject: function() {
            return "Two";
        },
        message: function() {
            return "Three";
        }
    };
    runButtonTest(
        Markup.privateMessageLink.name,
        "Lo" + $.getConfig().forumRoot + "index.php?fn=send_pm&manual_username=One&title=Two&message=Threerem ipsum",
        "Ip" + $.getConfig().forumRoot + "index.php?fn=send_pm&manual_username=One&title=Two&message=Threesum Lorem"
    );
    Markup.privateMessageLink._informationProvider = infoProvider;
    
    runButtonTest(Markup.privateMessageShortHand.name, "Lo[[pm:rem i]]psum", "Ip[[pm:sum L]]orem");
    runButtonTest(Markup.profile.name, "Lo[[member:rem i]]psum", "Ip[[member:sum L]]orem");
    
    infoProvider = Markup.replace._informationProvider;
    Markup.replace._informationProvider = {
        oldText: function() {
            return "Lorem";
        },
        newText: function() {
            return "Dolor";
        },
    };
    runButtonTest(Markup.replace.name, "Dolor ipsum", "Ipsum Dolor");
    Markup.replace._informationProvider = infoProvider;
    
    runButtonTest(Markup.strike.name, "Lo[strike]rem i[/strike]psum", "Ip[strike]sum L[/strike]orem");
    runButtonTest(Markup.subscript.name, "Lo[sub]rem i[/sub]psum", "Ip[sub]sum L[/sub]orem");
    runButtonTest(Markup.superscript.name, "Lo[sup]rem i[/sup]psum", "Ip[sup]sum L[/sup]orem");
});

test("Selects add correct tags", function() {
    for (var i in _markupBranch) {
        if (_markupBranch.hasOwnProperty(i)) {
            _markupBranch[i] = true;
        }
    }

    var div = $F("#txtOneBar");
    div.html("");
    div.append(Markup.create($F("#txtOne"), MarkupLocation.Post, Markup.all));

    div = $F("#txtTwoBar");
    div.html("");
    div.append(Markup.create($F("#txtTwo"), MarkupLocation.Post, Markup.all));

    // Custom messages:
    runSelectTest(
        "namfox-custom-messages-select",
        function (select, textarea, reset) {
            for (var i = 1; i < select.options.length - 1; ++i) {
                var option = select.options[i];
                var oldText = $.messages.getMessage(option.value).text;
                
                try {
                    $.messages.getMessage(option.value).text = "[cust" + i + "]#selected#[/cust" + i + "]";
                    select.selectedIndex = i;
                    change(select);
                }
                finally {
                    $.messages.getMessage(option.value).text = oldText;
                }
                
                var expected = "";
                
                if (textarea.id === "txtOne") {
                    expected = "Lo[cust" + i + "]rem i[/cust" + i + "]psum";
                }
                else {
                    expected = "Ip[cust" + i + "]sum L[/cust" + i + "]orem";
                }
                
                equals(expected, textarea.value, "The value should equal '" + expected + "' in the text area.");
                reset();
            }
            
            var oldText = $.messages.getDefaultMessage().text;
            
            try {
                $.messages.getDefaultMessage().text = "[default][/default]";
                select.selectedIndex = select.options.length - 1;
                change(select);
            }
            finally {
                $.messages.getDefaultMessage().text = oldText;
            }
            
            if (textarea.id === "txtOne") {
                expected = "Lo[default][/default]rem ipsum";
            }
            else {
                expected = "Ip[default][/default]sum Lorem";
            }
            
            equals(expected, textarea.value, "The value should equal '" + expected + "' in the text area.");
        }
    );
    
    // Videos:
    runSelectTest(
        "namfox-videos-select",
        function (select, textarea, reset) {
            for (var i = 1; i < select.options.length; ++i) {
                var option = select.options[i];
                select.selectedIndex = i;
                
                var open = "", close = "", transformer = undefined;
                switch (option.value) {
                    case "nv":
                        open = "[neovid]";
                        close = "[/neovid]";
                        break;
                    case "gs":
                        open = "[gamespot]";
                        close = "[/gamespot]";
                        transformer = $.transformers.gamespot;
                        break;
                    case "gt":
                        open = "[gametrailers]";
                        close = "[/gametrailers]";
                        transformer = $.transformers.gametrailers;
                        break;
                    case "gtu":
                        open = "[gametrailers_user]";
                        close = "[/gametrailers_user]";
                        transformer = $.transformers.gametrailersUser;
                        break;
                    case "gv":
                        open = "[gamevideos]";
                        close = "[/gamevideos]";
                        transformer = $.transformers.gamevideos;
                        break;
                    case "ign":
                        open = "[ignvideo]";
                        close = "[/ignvideo]";
                        break;
                    case "yt":
                        open = "[yt]";
                        close = "[/yt]";
                        break;
                }
                
                change(select);
                
                if (textarea.id === "txtOne") {
                    expected = "Lo" + open + "rem i" + close + "psum";
                }
                else {
                    expected = "Ip" + open + "sum L" + close + "orem";
                }
                equals(expected, textarea.value, "The value should equal '" + expected + "' in the text area.");
                
                var backup = Markup.addText;
                Markup.addText = function(spec, textarea, selectionRule, trans) {
                    equals(transformer, trans, "The expected transformer for the video tag must match the actual transformer.");
                };
                change(select);
                Markup.addText = backup;
                
                reset();
            }
        }
    );
});

}});